//
//  AddNumTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2022/8/5.
//

import UIKit

typealias BlockPopAddShoppingCarNum = () -> Void//添加到购物车
typealias BlockPopSubtractShoppingCarNum = () -> Void//减购物车数量
typealias BlockPopModifyShopingCarNum = (_ textField: UITextField) -> Void//修改购物车数量

class AddNumTableViewCell: UITableViewCell {
    
    var titleLab: UILabel!
    var addBtn: UIButton!
    var numTextField: UITextField!
    var subtractBtn: UIButton!
    var num = 1
    
    var addShoppingCarNumBlock: BlockPopAddShoppingCarNum!
    var subtractShoppingCarNumBlock: BlockPopSubtractShoppingCarNum!
    var modifyShopingCarNumBlock: BlockPopModifyShopingCarNum!
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        self.initUI()
        self.addLayout()
    }
    
    
    func initUI() {
        
        self.titleLab = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 15)
        self.titleLab.text = MMLocalizedString("quantity", comment: "数量")
        self.contentView.addSubview(self.titleLab)
        
        
        //添加按钮
        self.addBtn = UIButton(frame: CGRect(x:kScreenWidth - 38, y: 0, width: 24, height: 24))
        self.addBtn.setImage(UIImage(named: "icon_add_car"), for: .normal)
        self.addBtn.contentHorizontalAlignment = .right
        self.addBtn.addTarget(self, action: #selector(addBtnClick), for: .touchUpInside)
        self.contentView.addSubview(addBtn)
        
        self.numTextField = UITextField(frame: CGRect(x: self.addBtn.left - self.addBtn.width, y: 0, width: 24, height: 24))
        self.numTextField.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.numTextField.textColor = Theme.titleLabColor
        self.numTextField.textAlignment = .center
        self.numTextField.keyboardType = .numberPad
        self.numTextField.delegate = self
        self.contentView.addSubview(self.numTextField)


        
        //减按钮
        self.subtractBtn = UIButton(frame: CGRect(x:self.numTextField.left - self.numTextField.width, y: 0, width: 24, height: 24))
        self.subtractBtn.setImage(UIImage(named: "icon_subtract_car"), for: .normal)
        self.subtractBtn.contentHorizontalAlignment = .right
        self.subtractBtn.addTarget(self, action: #selector(subtractBtnClick), for: .touchUpInside)
        self.contentView.addSubview(subtractBtn)
   
        self.numTextField.text = "1"
        
    }
    
    
    func addLayout(){
        
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(18)
            make.height.equalTo(30).priority(.high)
            make.bottom.equalTo(-30)
        }
        
        
        self.addBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-18)
            make.width.height.equalTo(30)
            make.centerY.equalTo(self.titleLab.snp.centerY)
        }

        self.numTextField.snp.makeConstraints { (make) in
            make.right.equalTo(self.addBtn.snp.right).offset(-24)
            make.width.equalTo(35)
            make.height.equalTo(30)
            make.centerY.equalTo(self.titleLab.snp.centerY)
        }

        self.subtractBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self.numTextField.snp.left)
            make.width.height.equalTo(30)
            make.centerY.equalTo(self.titleLab.snp.centerY)
        }
        
        
    }
    
    
    
    
    //添加
    @objc func addBtnClick() {
        self.numTextField.text = String(format: "%ld", self.num)
        if self.addShoppingCarNumBlock != nil {
            self.addShoppingCarNumBlock()
        }
        
    }
    //减
    @objc func subtractBtnClick() {
        if (self.num > 1) {
            self.num = self.num - 1
            self.numTextField.text = String(format: "%ld", self.num)
        }else{
        }
        if self.subtractShoppingCarNumBlock != nil {
            self.subtractShoppingCarNumBlock()
        }
    }
    
    
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension AddNumTableViewCell: UITextFieldDelegate{
    func textFieldDidEndEditing(_ textField: UITextField) {
        if self.modifyShopingCarNumBlock != nil {
            self.modifyShopingCarNumBlock(textField)
        }
    }
    
}
